local skill = fk.CreateSkill {
  name = "sanshe__foresight_skill",
}

skill:addEffect("cardskill", {
  prompt = "#sanshe__foresight_skill",
  mod_target_filter = Util.TrueFunc,
  can_use = Util.CanUseToSelf,
  on_effect = function(self, room, effect)
    if effect.to.dead then return end
    effect.extra_data = effect.extra_data or {}
    local cards = room:getNCards(2)
    effect.extra_data.see = cards
    if effect.extra_data.show then
      room:showCards(cards)
      local get = room:askToArrangeCards(effect.to, {
        skill_name = skill.name,
        card_map ={cards,{},"Top","Bottom"},
        --prompt = "",
        free_arrange = true,
        box_size = 0,
        max_limit = {2, 2},
        --min_limit = {2, 2},
        default_choice = {cards, {}},
      })
      local top,bottom = get[1],get[2]
      for i = #top, 1, -1 do
        table.removeOne(room.draw_pile, top[i])
        table.insert(room.draw_pile, 1, top[i])
      end
      for i = 1, #bottom, 1 do
        table.removeOne(room.draw_pile, bottom[i])
        table.insert(room.draw_pile, bottom[i])
      end

      room:syncDrawPile()
      room:sendLog{
        type = "#GuanxingResult",
        from = effect.to.id,
        arg = #top,
        arg2 = #bottom,
      }
      if #top > 0 then
        room:sendLog{
          type = "#PutCardTop",
          card = top,
        }
      end
      if #bottom > 0 then
        room:sendLog{
          type = "#PutCardBottom",
          card = bottom,
        }
      end
    else
      room:askToGuanxing(effect.to, { cards = cards })
    end
    room:drawCards(effect.to, 2, skill.name)
    if effect.extra_data.afterfun and type(effect.extra_data.afterfun) == "function" then
      effect.extra_data.afterfun(effect)
    end
  end,
})

return skill
